package com.xhb.biz.job;

import com.xhb.biz.constant.UserConfigConstant;
import com.xhb.biz.feign.UserDataSyncService;
import com.xhb.biz.po.ConfigPO;
import com.xhb.biz.service.*;
import feign.Feign;
import feign.Request;
import feign.Retryer;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AopContext;
import org.noear.solon.schedule.annotation.Scheduled;

import java.util.Collections;
import java.util.Map;

/**
 * @author tanyukun
 */
@Component
//@Scheduled(cron = "0/10 * * * * ? ")
@Scheduled(cron = "0 0 4 1/1 * ? *")
public class UserDataSyncJob implements Runnable {

    @Inject
    ConfigService configService;
    @Inject("${manager.url}")
    String managerServerUrl;


    @Override
    public void run() {
        ConfigPO configPo = configService.selectUserConfigByKey(UserConfigConstant.USER_TOKEN_KEY);
        if (null == configPo) {
            return;
        }

        AopContext context = Solon.context();
        UserDataSyncService userService = Feign.builder()
                .options(new Request.Options(1000, 3500))
                .retryer(new Retryer.Default(5000, 5000, 3))
                .target(UserDataSyncService.class, managerServerUrl);

        Map<String, Object> headers = Collections.singletonMap(UserConfigConstant.USER_TOKEN_KEY, configPo.getContent());

        // 导航分组数据同步
        context.getBean(GroupService.class).userNavigationGroupListSync(userService, headers);

        // 导航分组数据同步
        context.getBean(NavigationService.class).userNavigationListSync(userService, headers);

        // 项目数据同步
        context.getBean(ProjectService.class).projectListSync(userService, headers);

        // 项目收藏夹数据同步
        context.getBean(ProjectUrlService.class).projectUrlListSync(userService, headers);

        // 登陆人收藏文件夹信息
        context.getBean(CollectGroupService.class).userCollectGroup(userService, headers);

        // 登陆人收藏数据同步
        context.getBean(CollectUrlService.class).userCollectUrl(userService, headers);

        // 代理和代理 url 数据
        context.getBean(ProxyService.class).proxyListSync(userService, headers);
    }


}
